#include "mytreeitemdelegate.h"
#include "mytreeview.h"
#include <QApplication>
#include <QObject>
MyTreeItemDelegate::MyTreeItemDelegate(int iconTextSpacing, QObject *parent)
    : QStyledItemDelegate{parent}
    , m_textColr(QColor("#B5B5BE"))
    , m_nVsapcing(10)
    , m_parentObject(parent)
    , m_backgroundColr(QColor("#1C1C24"))
{}

void MyTreeItemDelegate::setTextColor(QColor color)
{
    m_textColr = color;
}

void MyTreeItemDelegate::addItem(long long nIndex, QString icon, QString highticon)
{
   QVector<QString>& vecIcon = m_IconMap[nIndex];
   vecIcon.push_back(icon);
   vecIcon.push_back(highticon);
}

void MyTreeItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QStyleOptionViewItem opt = option;
    initStyleOption(&opt, index);
    // 计算左侧边距
    int leftMargin = 0; // 自定义的左侧边距大小

    painter->save();

    QFontMetrics fm(opt.font);
    int textHeight = fm.height();
    // 绘制背景 把默认画的东西都盖住
    QRect backRect = opt.rect;
    backRect.setLeft(0);

    QRect adjustedRect = opt.rect.adjusted(0, m_nVsapcing, 0, 0);
    opt.rect = adjustedRect;


    QBrush brush(m_backgroundColr);
    if (opt.state & QStyle::State_Selected) {
        painter->fillRect(backRect, brush);
        QRect selRect = backRect.adjusted(0, m_nVsapcing, 0, 0);;
        brush.setColor(QColor(Qt::white));
        selRect.setWidth(2);
        selRect.setHeight(textHeight);
        painter->fillRect(selRect, brush);
    } else {
        painter->fillRect(backRect, brush);
    }

    QRect iconRect = opt.rect.adjusted(leftMargin, 0, 0 , 0);
    iconRect.setWidth(textHeight);
    iconRect.setHeight(textHeight);

    MyTreeView * treeView = qobject_cast<MyTreeView *>(m_parentObject);
    QStandardItem * Item = treeView->getItem(index);
    int idindex =  Item->data(ItemRoles::IdRole).toInt();
    const QVector<QString> vecIcon = m_IconMap[idindex];
    // 绘制图标
    if(opt.state & QStyle::State_Selected)
    {
        QImage image(vecIcon[1]);
        painter->drawImage(iconRect, image);
    }
    else
    {
        QImage image(vecIcon[0]);
        painter->drawImage(iconRect, image);
    }

    // 设置文本的位置
    QRect textRect = opt.rect;
    textRect.setLeft(iconRect.right() + 10);
    painter->setBrush(Qt::NoBrush);
    // 绘制文本
    if (opt.state & QStyle::State_Selected)
    {
        painter->setPen(QColor(Qt::white));
    }
    else
    {
        painter->setPen(m_textColr);
    }

    // 设置对齐方式
    int flags = Qt::AlignTop | Qt::AlignLeft;

    painter->drawText(textRect, flags, opt.text);

    //绘制展开折叠箭头
    if(opt.state & QStyle::State_Children)
    {
        // 定义大于号的点
        if(treeView->isExpanded(index))
        {
            int nX = backRect.width() - 15;
            int nY = textRect.bottom() - 5;
            QPointF points[3] = {
                QPointF(nX - 5, nY - textHeight / 2),
                QPointF(nX, nY),
                QPointF(nX + 5, nY - textHeight / 2)
            };

            // 绘制大于号
            painter->drawPolyline(points, 3);

        }
        else
        {
            int nX = backRect.width() - 10;
            int nY = textRect.top() + textHeight / 2;
            QPointF points[3] = {
                QPointF(nX - 10, nY - textHeight / 2 + 5),
                QPointF(nX, nY),
                QPointF(nX - 10, nY + textHeight / 2 - 5)
            };

            // 绘制大于号
            painter->drawPolyline(points, 3);
        }
    }

    painter->restore();
}

void MyTreeItemDelegate::drawArrow(QPainter *painter, const QRect &rect, bool expanded) const
{
    return;
    QPolygonF polygon;
    if (expanded) {
        // 绘制向下箭头
        polygon << QPointF(rect.left(), rect.top() + rect.height() / 4)
                << QPointF(rect.right(), rect.top() + rect.height() / 4)
                << QPointF(rect.left() + rect.width() / 2, rect.bottom() - rect.height() / 4);
    } else {
        // 绘制向右箭头
        polygon << QPointF(rect.left() + rect.width() / 4, rect.top())
                << QPointF(rect.left() + rect.width() / 4, rect.bottom())
                << QPointF(rect.right() - rect.width() / 4, rect.top() + rect.height() / 2);
    }
    //painter->setBrush(option.palette.windowText());
    painter->drawPolygon(polygon);
}

QSize MyTreeItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QSize size = QStyledItemDelegate::sizeHint(option, index);
    size.setHeight(size.height() + m_nVsapcing); // 增加高度以包含行间距
    return size;
}
